<template>
  <el-dialog v-el-drag-dialog title="数据库文档" :visible.sync="visible" :append-to-body="appendToBody" fullscreen :before-close="onClose">
    <!-- 操作工作栏 -->
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" plain icon="el-icon-plus" @click="handleExportHtml">导出 HTML</el-button>
        <el-button type="primary" size="mini" plain icon="el-icon-plus" @click="handleExportWord">导出 Word</el-button>
        <el-button type="primary" size="mini" plain icon="el-icon-plus" @click="handleExportMarkdown">导出 Markdown</el-button>
      </el-col>
    </el-row>

    <div :style="'height:' + height">
      <iframe :src="src" frameborder="no" style="width: 100%; height: 100%" scrolling="auto" />
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'DbDocDialog',
  data() {
    return {
      visible: false,
      appendToBody: true,
      height: document.documentElement.clientHeight - 137 + 'px;',
      form: {
        dbKey: undefined
      },
      src: ''
    }
  },
  mounted: function () {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 137 + 'px;'
    }
  },
  methods: {
    init(dbKey) {
      this.form.dbKey = dbKey || 'master'
      this.visible = true
      // 加载 Html，进行预览
      this.$modal.loading('加载文档中，请稍候')
      let url = encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/dbase/doc/` + this.form.dbKey + '/html')
      this.$download.browse(url, 'post').then(response => {
        this.src = response
        this.$modal.closeLoading()
      }).catch((e) => {
        this.$modal.msgError(e)
        this.$modal.closeLoading()
      })
    },
    /** 处理导出 HTML */
    handleExportHtml() {
      let url = encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/dbase/doc/` + this.form.dbKey + '/html')
      this.$download.download(url)
    },
    /** 处理导出 Word */
    handleExportWord() {
      let url = encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/dbase/doc/` + this.form.dbKey + '/doc')
      this.$download.download(url)
    },
    /** 处理导出 Markdown */
    handleExportMarkdown() {
      let url = encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/dbase/doc/` + this.form.dbKey + '/md')
      this.$download.download(url)
    },
    onClose() {
      this.form.dbKey = undefined
      this.url = ''
      this.visible = false
    }
  }
}
</script>
